/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.com.oceansoft.modules.demo;


import cn.com.oceansoft.framework.kits.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zxh
 * @version 2016-09-12
 */
@Controller
@RequestMapping(value = "demos")
@Api(value = "示例",description = "演示API接口")
public class DemoController {

	/**
	 * 获取列表
	 * 如果有pi（pageIndex）或者ps(pageSize)参数，应该按分页来返回数据，否则返回所有
	 * @return
     */
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET)
	@ApiOperation(value="获取列表", notes="获取列表更多说明")
	public ApiResult get(Demo demo, HttpServletRequest request, HttpServletResponse response){

		//Page中封装ps,pi,sd,sb,kw等参数
		//Page<Demo> page=new Page<>(request,response);
		List<Demo> list=new ArrayList<>();
		//TODO 查询列表 返回list而不是page
		return new ApiResult(true, 200, "", list);
	}

	/**
	 * 获取分页数据
	 * @param demo
	 * @param request
	 * @param response
     * @return
     */
	@ResponseBody
	@RequestMapping(value="other/page",method = RequestMethod.GET)
	@ApiOperation(value="获取分页", notes="获取分页更多说明")
	public ApiResult page(Demo demo, HttpServletRequest request, HttpServletResponse response){
		//Page中封装ps,pi,sd,sb,kw等参数
//		Page<Demo> page=new Page<>(request,response);
		//TODO 查询列表
		return new ApiResult(true, 200, "", null);
	}
	/**
	 * 获取子列表，一般允许冗余 ，在SubDemoController中的 /sub_demos也能获取列表，
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="{demo_id}/sub_demos",method = RequestMethod.GET)
	@ApiOperation(value="获取子列表", notes="获取子列表更多说明")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="path",name="demo_id",dataType="String",required=true,value="示例ID")
	})
	public ApiResult get(@PathVariable("demo_id") String demo_id, SubDemo subDemo, HttpServletRequest request, HttpServletResponse response){
		List<Api> list = new ArrayList<>();
		//TODO 查询列表
		return new ApiResult(true, 200, "", list);
	}

	/**
	 * 创建
	 * @param demo
	 * @return
     */
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value="创建",notes = "创建新的示例")
	public ApiResult post(@RequestBody Demo demo){
		//TODO 新建demo
		//使用JSR-303 Validation进行验证
//		ApiResult r=beanValidatorApiResult(demo);
//		if(!r.isSucc()){
//			return r;
//		}
		return new ApiResult(true, 200, "", demo);
	}

	/**
	 * 获取单个对象
	 * @param id
	 * @return
     */
	@ResponseBody
	@RequestMapping(value="{id}",method = RequestMethod.GET)
	@ApiImplicitParam(paramType="path",name="id",dataType="String",required=true,value="示例ID")
	@ApiOperation(value="获取单个对象", notes="根据id获取对象")
	public ApiResult get(@PathVariable String id){
		Demo demo=null;
		//TODO 获取demo
		return new ApiResult(true, 200, "", demo);
	}

	/**
	 * 删除
	 * @param id
	 * @return
     */
	@ResponseBody
	@RequestMapping(value="{id}",method = RequestMethod.DELETE)
	@ApiImplicitParam(paramType="path",name="id",dataType="String",required=true,value="示例ID")
	@ApiOperation(value="删除", notes="删除所提供id的示例")
	public ApiResult delete(@PathVariable String id){
		//TODO 删除demo
		return new ApiResult();
	}

	/**
	 * 更新，客户端提供demo全部信息
	 * @param id
	 * @param demo
     * @return
     */
	@ResponseBody
	@RequestMapping(value="{id}",method = RequestMethod.PUT)
	@ApiImplicitParam(paramType="path",name="id",dataType="String",required=true,value="示例ID")
	@ApiOperation(value="更新", notes="客户端提供全部信息")
	public ApiResult put(@PathVariable String id, @RequestBody Demo demo){
		//TODO 更新demo
		return new ApiResult(true, 200, "", demo);
	}

	/**
	 * 更新，客户端提供demo部分信息
	 * @param id
	 * @param demo
     * @return
     */
	@ResponseBody
	@RequestMapping(value="{id}",method = RequestMethod.PATCH)
	@ApiImplicitParam(paramType="path",name="id",dataType="String",required=true,value="示例ID")
	@ApiOperation(value="更新", notes="客户端提供部分信息")
	public ApiResult patch(@PathVariable String id, @RequestBody Demo demo){
		//TODO 更新demo demo提供部分属性
		//BeanUtils.copyPropertiesIgnoreNull(olddemo,demo);
		return new ApiResult(true, 200, "", demo);
	}

	/**
	 * 根据过滤条件返回数量
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="other/count",method = RequestMethod.GET)
	@ApiOperation(value="获取数量", notes="根据过滤条件返回数量")
	public ApiResult count(HttpServletRequest request, HttpServletResponse response){
		return new ApiResult(true, 200, "", 0);
	}
	/**
	 * 求和
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="other/sum",method = RequestMethod.GET)
	@ApiOperation(value="求和", notes="求字段的和")
	public ApiResult sum(HttpServletRequest request, HttpServletResponse response){
		return new ApiResult(true, 200, "", 0);
	}

	@ApiIgnore
	@ResponseBody
	public ApiResult ignore(){
		return new ApiResult();
	}
}